package com.example.module_filemanage.MVVM.ViewModule;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Toast;

import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.example.module_filemanage.Adapter.FileInfo_Adapter;
import com.example.module_filemanage.Adapter.LeftMenu_Adapter;
import com.example.module_filemanage.Interface.ICallBack;
import com.example.module_filemanage.MVVM.Module.Entil.Item_FileInfo_module;
import com.example.module_filemanage.MVVM.Module.QiNiu.FileManager;
import com.example.module_filemanage.MVVM.Module.QiNiu.FileManager2;
import com.example.module_filemanage.MVVM.View.Dialog.MDialog;
import com.example.module_filemanage.R;
import com.example.module_filemanage.databinding.FmSpaceFragmentBinding;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;

/**
 * Created by SJ on 2018/4/20.
 */

public class FMFile_Info_VM {
    Context mContext;
    FmSpaceFragmentBinding mBinding;
    public FileInfo_Adapter _fileInfo_adapter=new FileInfo_Adapter(null);
    private RecyclerAdapterWithHF mAdapter;
    ProgressDialog mProgressDialog;
    public FMFile_Info_VM(Context context, FmSpaceFragmentBinding binding, ProgressDialog waitingDialog){
        this.mContext=context;
        this.mBinding=binding;
        this.mProgressDialog=waitingDialog;
    }
    public void init(final String spacename){
        //private void setLeftMenuUI

        FileManager.getInstance(mContext.getApplicationContext()).getSpaceFileInfo(spacename, new ICallBack<ArrayList<Item_FileInfo_module>>() {
            @Override
            public void success(ArrayList<Item_FileInfo_module> items) {
                mProgressDialog.dismiss();
                mAdapter=new RecyclerAdapterWithHF( _fileInfo_adapter);
                _fileInfo_adapter.setItems(items);
                LinearLayoutManager layoutmanager = new LinearLayoutManager(mContext);
                //设置RecyclerView 布局
                mBinding.recFileinfo.setLayoutManager(layoutmanager);
                mBinding.recFileinfo.setAdapter(mAdapter);
                mBinding.refreshSpace.mPtrClassicHeader.setRefreshIcon(mContext.getDrawable(R.drawable.ic_refresh));
                mBinding.refreshSpace.mPtrClassicHeader.setRefreshTextColor(mContext.getResources().getColor(R.color.darkslateblue));
                addLayoutAnimation(mBinding.recFileinfo, LayoutAnimationController.ORDER_NORMAL, R.anim
                        .zoom_in);

            }
        });
        mBinding.refreshSpace.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                FileManager.getInstance(mContext.getApplicationContext()).getSpaceFileInfo(spacename, new ICallBack<ArrayList<Item_FileInfo_module>>() {
                    @Override
                    public void success(ArrayList<Item_FileInfo_module> items) {
                        mBinding.refreshSpace.refreshComplete();
                        _fileInfo_adapter.setItems(items);
                        mAdapter.notifyDataSetChanged();
                        addLayoutAnimation(mBinding.recFileinfo, LayoutAnimationController.ORDER_NORMAL, R.anim
                                .zoom_in);
                    }


                });
            }
        });
    }
    /**
     * 为ViewGroup对象添加动画
     * @param viewGroup 添加动画的布局
     * @param layoutOrder 动画播放的模式
     * @param anim  动画的属性文件
     */
    private void addLayoutAnimation(ViewGroup viewGroup, int layoutOrder, int anim) {
        LayoutAnimationController lac = new
                LayoutAnimationController(AnimationUtils.loadAnimation(mContext, anim));
        lac.setOrder(layoutOrder);
        viewGroup.setLayoutAnimation(lac);
        viewGroup.startLayoutAnimation();
    }
}
